﻿using System;
using System.Diagnostics;
using System.IO;

namespace LoveMusic.Helpers
{
    public class TraceLog
    {
        #region Fields
        // ============================================================
        public static string Name = "ILoveThisMusicLogListener";
        public static string FileName = "ILoveThisMusicLogListener.log";
        public static bool bPrintToConsole = true;


        private static TextWriterTraceListener m_Listener;
        // ============================================================
        #endregion

        #region Initialization
        // ============================================================
        /// <summary>
        /// Type initializer for TraceLog
        /// </summary>
        static TraceLog()
        {
            
            if (Trace.Listeners.Count > 2)
            {
                m_Listener = Trace.Listeners[1] as TextWriterTraceListener;
            }
            else
            {
                TextWriterTraceListener listener = Trace.Listeners[TraceLog.Name] as TextWriterTraceListener;

                if (listener == null)
                {
                    try
                    {
                        FileStream objStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/TraceLog.txt"), FileMode.Append);

                        listener = new TextWriterTraceListener(objStream);
                        Trace.Listeners.Add(listener);
                    }
                    catch (Exception ex)
                    {
                        return;
                    }
                }

                m_Listener = listener;
            }
        }
        // ============================================================
        #endregion

        #region Properties
        // ============================================================
        protected static TextWriterTraceListener Listener
        {
            get
            {
                return TraceLog.m_Listener;
            }
        }
        // ============================================================
        #endregion

        #region Methods
        // ============================================================
        public static void Write(string message)
        {
            if (m_Listener == null) { return; }

            m_Listener.WriteLine("\t >> " + message);
        }

        public static void TimeWrite(string message)
        {
            if (m_Listener == null)
            { return; }

            string
                date = DateTime.Now.ToShortDateString(),
                time = DateTime.Now.ToShortTimeString(),
                logMessage = string.Format("\n\n\r{0}, [{1}] >> {2}", date, time, message, "");

            m_Listener.WriteLine(logMessage);
            m_Listener.Flush();
            if (bPrintToConsole)
            {
                Console.WriteLine(logMessage);
            }
        }

        public static void TimeWrite(Exception exception)
        {
            TraceLog.TimeWrite(exception, null);
        }

        public static void TimeWrite(Exception exception, string method)
        {
            if (m_Listener == null) { return; }

            string
                date = DateTime.Now.ToShortDateString(),
                time = DateTime.Now.ToShortTimeString(),
                logMessage = null;

            if (!string.IsNullOrEmpty(method))
            {
                logMessage = string.Format("\n\r{0}, [{1}] >> EXCEPTION in {2}: {3}\n\r>>\t{4}", date, time, method, exception.Message, "");
            }
            else
            {
                logMessage = string.Format("\n\r{0}, [{1}] >> EXCEPTION: {2}\n\r>>\t{3}", date, time, exception.Message, "");
            }

            m_Listener.WriteLine(logMessage);
            m_Listener.Flush();
            m_Listener.Close();
        }

        private static void FileWrite(Exception exception, string method)
        {
            //string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/Signup.txt");
            //body = System.IO.File.ReadAllText(file);

            FileStream objStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/TraceLog.txt"), FileMode.Append);
            TextWriterTraceListener objTraceListener = new TextWriterTraceListener(objStream) ;
            Trace.Listeners.Add(objTraceListener);
            Trace.WriteLine("Hello 15Seconds Reader -- This is first trace message");
            Trace.WriteLine("Hello again -- This is second trace message") ;
            Debug.WriteLine("Hello again -- This is first debug message") ;
            //Trace.Flush();
            objStream.Close();
        }
        /// <summary>
        /// Explicit flush
        /// </summary>
        public static void Flush()
        {
            m_Listener.Flush();
        }
        // ============================================================
        #endregion
    }
}
